import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  }// ,
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [
  //     {
  //       path: 'dashboard',
  //       component: () => import('@/views/dashboard/index'),
  //       name: 'Dashboard',
  //       meta: { title: '首页', icon: 'dashboard', affix: true }
  //     }
  //   ]
  // }
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/staff/lent',
    meta: { title: '', icon: 'peoples', roles: ['staff'] },
    children: [
      {
        path: '/staff/lent',
        component: () => import('@/views/staff/lent'),
        name: 'Dashboard',
        meta: { title: '借阅书籍', icon: '', affix: true, roles: ['staff'] }
      }
    ]
  },
  {
    path: '/staff',
    component: Layout,
    redirect: '/staff/return',
    meta: { title: '', icon: 'peoples', roles: ['staff'] },
    children: [
      {
        path: '/staff/return',
        component: () => import('@/views/staff/return'),
        meta: { title: '归还书籍', icon: '', affix: true, roles: ['staff'] }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '图书搜索', icon: 'dashboard', affix: true, roles: ['boss', 'reader'] }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/admin/book_on_shelf',
    meta: { title: '', icon: 'peoples', roles: ['boss'] },
    children: [{
      path: '/dashboard/admin/book_on_shelf',
      component: () => import('@/views/admin/book_on_shelf'),
      meta: { title: '图书入库', icon: 'peoples', roles: ['boss'] }
    }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/admin/book_off_shelf',
    meta: { title: '', icon: 'peoples', roles: ['boss'] },
    children: [{
      path: '/dashboard/admin/book_off_shelf',
      component: () => import('@/views/admin/book_off_shelf'),
      meta: { title: '图书下架', icon: 'peoples', roles: ['boss'] }
    }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/admin/reader_management',
    meta: { title: '', icon: 'peoples', roles: ['boss'] },
    children: [{
      path: '/dashboard/admin/reader_management',
      component: () => import('@/views/admin/reader_management'),
      meta: { title: '读者管理', icon: 'peoples', roles: ['boss'] }
    }
    ]
  },
  {
    path: '/reader',
    component: Layout,
    redirect: '/reader/readerHome',
    meta: { title: '', icon: 'peoples', roles: ['reader'] },
    children: [{
      path: '/reader/readerHome',
      component: () => import('@/views/reader/readerHome'),
      meta: { title: '个人中心', icon: 'peoples', roles: ['reader'] }
    }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
